package com.ishansong.ticket.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.matrixjoy.dal.config.GroupConfig;
import com.matrixjoy.dal.dao.DBAgent;
import com.matrixjoy.dal.dao.impl.DBAgentHibernateImpl;
import com.matrixjoy.dal.dao.model.SqlInfo;
import com.matrixjoy.dal.dao.util.SqlUtil;
import com.matrixjoy.dal.datasource.ContextHolder;
import com.matrixjoy.dal.route.strategy.IStrategy;

/**
 * 
 * @author hongtaozhang
 * 
 */
public class DBUtil {

	public static DBAgent dbAgent = DBAgentHibernateImpl.getInstance();
	public static List<String> dataSourcesList = new ArrayList<String>();

	static {
		
		Map<Object, Object> dataSourcesMap = GroupConfig.getInstance().getDataSourceItemMap();
		for (Iterator<Object> it = dataSourcesMap.values().iterator(); it.hasNext();) {
			Object ds = it.next();

			String dsStr = String.valueOf(ds);
			if (!dsStr.equals("idCenterDS")) {
				dataSourcesList.add(String.valueOf(dsStr));
			}
		}
	}

	/**
	 * 分库聚合查询ids列表
	 * @param account_id
	 * @param listName
	 * @param params
	 * @return
	 */
	public static List<Object> getAggregationIdList(Object account_id, final String listName, Object[] params) {
		
		SqlInfo sqlInfo = SqlUtil.getListSql(listName, params);
		List<Object> objList = new ArrayList<Object>();

		 Lock lock = new ReentrantLock(); // 锁
		 lock.lock();
		for (String ds : dataSourcesList) {
			
			ContextHolder.setDataSource(ds);
			Integer count = dbAgent.count(account_id, listName, sqlInfo);
			List<Object> ids = dbAgent.getIdList(account_id, listName, sqlInfo, 0, count);
			if( null != ids && ids.size() > 0 ){
				objList.addAll(ids);	
			}
		}
		lock.unlock();
		return objList;
	}

	/**
	 * 分库查询对象列表
	 * @param account_id
	 * @param cls
	 * @param listName
	 * @param params
	 * @return
	 */
	public static List<Object> getAggregationList(Object account_id, Class<?> cls, final String listName, Object[] params) {

		SqlInfo sqlInfo = SqlUtil.getListSql(listName, params);
		List<Object> objList = new ArrayList<Object>();
		
		 Lock lock = new ReentrantLock(); // 锁
		 lock.lock();
		for (String ds : dataSourcesList) {
			
			ContextHolder.setDataSource(ds);
			List<Object> entityLs = dbAgent.getEntityList(account_id, cls, sqlInfo, IStrategy.STRATEGY_R);
			if( null != entityLs && entityLs.size() > 0 ){
				objList.addAll(entityLs);	
			}
		}
		lock.unlock();
		return objList;
	}
	
//	
//
//    List objList = new ArrayList();
//
//    ObjectItem objItem = DaoHelper.getObjectItemByObjectName(clazz.getName());
//    String baseSql = objItem.getEntitySql();
//    if (null == baseSql || StringUtils.isEmpty(baseSql)) {
//        return objList;
//    }
//    List<SqlInfo> sqlInfoLs = SqlUtil.getEntityUnionSql(clazz, ids, baseSql);
//    if (null == sqlInfoLs || 0 == sqlInfoLs.size()) {
//        return objList;
//    }
//
//    RoutingService.getInstance().setRoutingStrategyForObject(clazz, account_id, ids.get(0), IStrategy.STRATEGY_R);
//
//    for (SqlInfo sqlInfo : sqlInfoLs) {
//        List entityLs = dbAgent.getEntityList(account_id, clazz, sqlInfo, IStrategy.STRATEGY_R);
//        objList.addAll(entityLs);
//    }
//    return objList;
}
